package com.arcpan.Service.Impl;

import com.arcpan.Constant.BaseConstant;
import com.arcpan.Entity.Dto.SysSettingDto;
import com.arcpan.Entity.EmailCode;
import com.arcpan.Entity.UserInfo;
import com.arcpan.Exception.BaseException;
import com.arcpan.Mapper.EmailCodeMapper;
import com.arcpan.Mapper.UserInfoMapper;
import com.arcpan.Service.EmailCodeService;
import com.arcpan.Util.StringTools;
import com.arcpan.component.RedisComponent;
import com.arcpan.config.AppConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.Date;

@Service
public class EmailCodeServiceImpl implements EmailCodeService {
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private EmailCodeMapper emailCodeMapper;
    @Resource
    private JavaMailSender javaMailSender;
    @Resource
    private RedisComponent redisComponent;
    private static final Logger logger= LoggerFactory.getLogger(EmailCodeServiceImpl.class);
    @Resource
    private AppConfig appConfig;
@Transactional(rollbackFor = Exception.class)
    public  void sendEmailCode(String email, Integer type){
        if(type  == 0){
            UserInfo userInfo=userInfoMapper.selectByEmail(email);
            if(null != userInfo){
                throw new BaseException("邮箱已经存在");
            }
        }
        String code= StringTools.getRandomNumber(BaseConstant.LENGTH_5);
    //发送验证码
    sendMailCode(email,code);
        //禁用验证码
        emailCodeMapper.disableEmailCode(email);
        EmailCode emailCode=new EmailCode();
        emailCode.setCode(code);
        emailCode.setEmail(email);
        emailCode.setStatus(BaseConstant.ZERO);
        emailCode.setCreateTime(new Date());
        emailCodeMapper.insert(emailCode);

    }
    private void sendMailCode(String toEmail,String code){
    try {
        MimeMessage message=javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(message,true);
        mimeMessageHelper.setFrom(appConfig.getSendUserName());

        mimeMessageHelper.setTo(toEmail);
        SysSettingDto sysSettingDto= redisComponent.getsysSettingDto();

        mimeMessageHelper.setSubject(sysSettingDto.getRegisterMailTitle());
        mimeMessageHelper.setText(String.format(sysSettingDto.getRegisterEmailContent(),code));
        mimeMessageHelper.setSentDate(new Date());
        javaMailSender.send(message);


    }catch (Exception e){
        logger.error("发送邮件失败",e);
        System.out.println(e);
        throw new BaseException("邮件发送失败");

    }


    }
    public  void checkCode(String email, Integer emailCode){
    EmailCode emailCode1=emailCodeMapper.selectByEmailAndCode(email,emailCode);
    if(null == emailCode1){
        throw new BaseException("邮箱验证码不正确");
    }
    if(emailCode1.getStatus() ==1 || System.currentTimeMillis()-emailCode1.getCreateTime().getTime()>BaseConstant.LENGTH_15*1000*60){
        throw new BaseException("验证码过期");

    }


    }
}
